package com.sunyard.layoutit.service;

import java.util.HashMap;
import java.util.Map;

import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.ext.freemarker.TemplateRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.service.StatusService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sunyard.layoutit.common.ApplicationConfig;

import freemarker.template.Configuration;

/**
 * 当页面或资源找不到是跳转到404页面
 * @author gdl
 *
 */
public class SimpleStatusService extends StatusService{

	private static final Logger logger = LoggerFactory.getLogger(SimpleStatusService.class);


	@Get
	public Representation getRepresentation(Status status, Request request,Response response) {
		String resourceRef = request.getResourceRef().getIdentifier();
//		logger.info("找不到资源 {}",resourceRef);
		String templateName = "404.ftl";
		Configuration config = ApplicationConfig.getFreemarkerConfig() ;
		TemplateRepresentation result = new TemplateRepresentation(
				templateName, config, MediaType.TEXT_HTML);
		Map<String, Object> dataModel = new HashMap<String, Object>();
		dataModel.put("resourceRef", resourceRef);
		result.setDataModel(dataModel);
		result.setCharacterSet(ApplicationConfig.REST_CHARACTER_SET);
//		response.setStatus(Status.SUCCESS_OK);
		
		return result;
	}
}